<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <template #header>
          <TreeTools root :node-data="company" @add="addFn" />
        </template>
        <!-- 树形组件 -->
        <el-tree :data="list" :props="defaultProps" default-expand-all>
          <template v-slot="{data}">
            <TreeTools
              :node-data="data"
              @getDepartments="getDepartments"
              @add="addFn"
              @edit="editFn"
            />
          </template>
        </el-tree>
      </el-card>
      <AddDept
        ref="add"
        :show-dialog.sync="showDialog"
        :node-data="nodeData"
        @getDepartments="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/TreeTools'
import AddDept from './components/AddDept.vue'
import { getDepartmentList } from '@/api/departments'
import { listToTree } from '@/utils/index'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      list: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {
        name: '',
        manager: '负责人',
        id: ''
      },
      loading: false,
      showDialog: false,
      nodeData: {}
    }
  },
  async created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const data = await getDepartmentList()
      this.company.name = data.companyName
      this.list = listToTree(data.depts)
      this.loading = false
    },
    addFn(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
    },
    editFn(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      // console.log(this.$refs.add.form)
      // this.$refs.add.form = { ...this.nodeData }
      this.$refs.add.getInfo(nodeData.id)
    }
  }
}
</script>

<style scoped lang="scss">
  .tree-card {
    padding: 30px 140px;
    font-size:14px;
    .box {
      width: 80px;
    }
  }
  ::v-deep {
    .el-card__header {
        padding: 0px 20px;
    }

    // 修改叶子节点
    // .el-tree-node__expand-icon
    // .el-tree-node__expand-icon.is-leaf::before {

    // }
    // .el-tree-node__expand-icon
    // .el-tree-node__expand-icon.expanded::before {
    //   content: "\e722";
    // }
    // 树状菜单的所有图标
    .el-tree-node__expand-icon {
      font-size: 18px;
      color: #000;
      &::before {
        content: "\e723";
      }
      // 展开的图标
      &.expanded {
        transform: rotate(0deg);
        &::before {
          content: "\e722";
        }
      }
      &.is-leaf {
        color: #000;
        &::before {
          content: "\e7ab";
        }
      }
    }
  }
</style>
